/**
 * Copyright 2020 OPSLI 快速开发平台 https://www.opsli.com
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.opsliframework.boot.oss.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.opsliframework.boot.oss.FileAttr;
import org.opsliframework.boot.oss.conf.LocalConfig;
import org.opsliframework.boot.oss.service.LocalService;
import org.opsliframework.boot.oss.tool.FileTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

/**
 * 本地文件上传
 *
 * @author Parker
 * @date 2021年4月30日14:09:08
 */
@Slf4j
@Service
public class LocalStorageServiceImpl implements LocalService {

    /** 固定路径 */
    private static final String FIXED_PATH = "files";


    /**
     * 文件上传
     *
     * @param file 文件
     * @return
     */
    @Override
    public FileAttr upload(File file , LocalConfig localConfig) {
        // 非空判断
        if(FileUtil.isEmpty(file)){
            return new FileAttr();
        }


        // 验证对象
//        ValidatorUtil.verify(config);

        // 当前时间戳
        long currentTimeMillis = System.currentTimeMillis();

        // 当前时间
        Date currDate = DateUtil.date(currentTimeMillis);

        // 静态路径
        String genPath =config.getUploadPath()+FileUtil.getUserHomePath();

        // 静态路径前缀 默认为空
        String pathPrefix = StrUtil.isNotEmpty(config.getPathPrefix())?config.getPathPrefix():"";

        // 文件夹名称
        String folderName = DateUtil.format(currDate, "yyyyMMdd");

        // 包 全名称
        String packageName = FileTool.handlePath(genPath, false)
                + FileTool.handlePath(FIXED_PATH,true) + FileTool.handlePath(pathPrefix,true)
                + FileTool.handlePath(folderName,true);
        // 包 半名称
        String packageNameByHalf = FileTool.handlePath(FIXED_PATH,true) + FileTool.handlePath(pathPrefix,true)
                + FileTool.handlePath(folderName,true);

        // 文件属性
        FileAttr fileAttr = FileTool.getFileAttr(file);
        // 设置文件路径
        fileAttr.setFileStoragePath(
                config.getDomain() + packageNameByHalf + fileAttr.getName() + fileAttr.getSuffix());

        // 创建文件夹
        FileUtil.mkdir(packageName);

        // 创建文件
        File tmpFile = new File(
                packageName + FileTool.handlePath(fileAttr.getRandomFileNameAndSuffix(),true));
        try {
            FileUtils.copyFile(file, tmpFile);
        }catch (IOException e){
            log.error(e.getMessage(), e);
        }
        FileUtil.touch(tmpFile);

        return fileAttr;
    }

    @Autowired
    public LocalConfig config;

    /**
     * 输入流上传
     * @param inputStream    输入流
     * @param suffix 后缀
     * @return
     */
    @Override
    public FileAttr upload(InputStream inputStream, String suffix , LocalConfig localConfig) {
        // 获得系统配置信息

        // 验证对象
//        ValidatorUtil.verify(config);

        // 当前时间戳
        long currentTimeMillis = System.currentTimeMillis();

        // 当前时间
        Date currDate = DateUtil.date(currentTimeMillis);

        // 静态路径
//        String genPath = config.getUploadPath()+ FileUtil.getUserHomePath();
        String genPath = config.getUploadPath();

        // 静态路径前缀 默认为空
        String pathPrefix = StrUtil.isNotEmpty(config.getPathPrefix())?config.getPathPrefix():"";

        // 文件夹名称
        String folderName = DateUtil.format(currDate, "yyyyMMdd");

        // 包 全名称
        String packageName = FileTool.handlePath(genPath, false)
                + FileTool.handlePath(FIXED_PATH,true) + FileTool.handlePath(pathPrefix,true)+ FileTool.handlePath(config.getCustomPath(),true)
                + FileTool.handlePath(folderName,true);
        // 包 半名称
        String packageNameByHalf = FileTool.handlePath(FIXED_PATH,true) + FileTool.handlePath(pathPrefix,true)+ FileTool.handlePath(config.getCustomPath(),true)
                + FileTool.handlePath(folderName,true);

        // 文件属性
        FileAttr fileAttr = FileTool.getFileAttr(inputStream, suffix);

        // 设置文件路径
        fileAttr.setFileStoragePath(
                config.getDomain() + packageNameByHalf
                        + FileTool.handlePath(fileAttr.getRandomFileNameAndSuffix(),true));

        // 创建文件夹 /Users/apple/Desktop/opsli-ui/opsli-boot-cloud2/static/Users/apple/static/files/qianzui/20240927
        FileUtil.mkdir(packageName);

        // 创建文件
        File tmpFile = new File(
                packageName + FileTool.handlePath(fileAttr.getRandomFileNameAndSuffix(),true));
        try {
            FileUtils.copyInputStreamToFile(inputStream, tmpFile);
        }catch (IOException e){
            log.error(e.getMessage(), e);
        }
        FileUtil.touch(tmpFile);

        //http://127.0.0.1:8000//static/files/qianzui/20240927/1727730305638015871E4QF.jpg
        return fileAttr;
    }
}
